﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace WhatCatch.Core
{
    /// <summary>
    /// 数据容器 负责数据的存取策略
    /// </summary>
    public class DataContainer: DataContainerBase
    {
        object lockList = new object();
        object lockDetail = new object();
        object lockTask = new object();
        object lockDetailTask = new object();

        //List<string> listUrl;
        //List<string> detailUrl;
        //Dictionary<string, int> dictListUrl;
        //Dictionary<string, int> dictDetailUrl;
        Hashtable hashListUrl;
        Hashtable hashDetailUrl;

        //List Dictionary Hashtable 在数量为1w时体现不出来区别
        //Dictionary 在用lock和ConcurrentDictionary中体现不出来区别。至少不用lock，在list.Any()判断时可能会发生异常

        /// <summary>
        /// 任务队列
        /// </summary>
        Queue<string> queueTask;
        /// <summary>
        /// 任务队列
        /// </summary>
        Queue<string> queueDetailTask;

        public DataContainer()
        {
            //listUrl = new List<string>();
            //detailUrl = new List<string>();
            //dictListUrl = new Dictionary<string, int>();
            //dictDetailUrl = new Dictionary<string, int>();
            hashListUrl = new Hashtable();
            hashDetailUrl = new Hashtable();

            queueTask = new Queue<string>();
            queueDetailTask= new Queue<string>();
        }

        //public bool IsExistInListUrl(string url)
        //{
        //    //跳过重复
        //    lock (lockList)
        //    {
        //        //if (listUrl.Any(c => c == url))
        //        //    return true;
        //        if (hashListUrl.ContainsKey(url))
        //            return true;
        //    }
        //    return false;
        //}

        public void AddListUrl(string url)
        {
            lock (lockList)
            {
                //判重
                if (hashListUrl.ContainsKey(url))
                    return;

                //listUrl.Add(url);
                hashListUrl.Add(url,0);
            }

            //入任务队列
            lock (lockTask)
            {
                queueTask.Enqueue(url);
            }
        }

        //public bool IsExistInDetailUrl(string url)
        //{
        //    lock (lockDetail)
        //    {
        //        //if (detailUrl.Any(c => c == url))
        //        //    return true;
        //        if (hashDetailUrl.ContainsKey(url))
        //            return true;
        //    }
        //    return false;
        //}

        public void AddDetailUrl(string url)
        {
            lock (lockDetail)
            {
                if (hashDetailUrl.ContainsKey(url))
                    return;

                //detailUrl.Add(url);
                hashDetailUrl.Add(url,0);
            }

            lock (lockDetailTask)
            {
                queueDetailTask.Enqueue(url);
            }
        }

        //public void EnqueueTask(string url)
        //{
            
        //}

        public string GetOneTask()
        {
            string url = "";
            lock (lockTask)
            {
                if (queueTask.TryDequeue(out url) == false)
                {
                    return null;
                }
            }
            return url;
        }

        ///// <summary>
        ///// 插入明细任务
        ///// </summary>
        ///// <param name="url"></param>
        //public void EnqueueDetailTask(string url)
        //{
        //    lock (lockDetailTask)
        //    {
        //        queueDetailTask.Enqueue(url);
        //    }
        //}
        /// <summary>
        /// 获取一个明细任务
        /// </summary>
        /// <returns></returns>
        public string GetOneDetailTask()
        {
            string url = "";
            lock (lockDetailTask)
            {
                if (queueDetailTask.TryDequeue(out url) == false)
                {
                    return null;
                }
            }
            return url;
        }
    }
}
